package com.atguigu.jinx.controller;

import com.atguigu.jinx.entity.dto.User;
import com.atguigu.jinx.entity.vo.*;
import com.atguigu.jinx.enums.SmsTemplateStatus;
import com.atguigu.jinx.job.ClearUselessPicture;
import com.atguigu.jinx.service.IJobTaskService;
import com.atguigu.jinx.service.IUserService;
import com.atguigu.jinx.service.impl.TencentCosService;
import com.atguigu.jinx.utils.PageUtils;
import com.atguigu.jinx.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.Pattern;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Description;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Objects;

import static com.atguigu.jinx.utils.NameConstants.SMS_SEND_FAILED;
import static com.atguigu.jinx.utils.NameConstants.SMS_SEND_SUCCESS;

/**
 * <p>
 * 我的任务
 * </p>
 *
 * @author steve
 * @since 2025-08-07
 */
@RestController
@CrossOrigin
@RequestMapping("/jinx/user")
@RequiredArgsConstructor
@Validated
public class UserController {

    private final IJobTaskService jobTaskService;

    private final IUserService userService;

    private final IJobTaskService iJobTaskService;

    private final TencentCosService tencentCosService;


    /**
     * 用户任务招标的列表图
     */
    @Description("任务招标的列表图")
    @PostMapping({"/page"})
    public R taskListPage(@RequestBody JobQueryParamVO param) {
        PageUtils jobVOPage = jobTaskService.taskListPageByUser(
                new Page<>(param.getPage(), param.getSize()), param);
        return R.ok().put("data", jobVOPage);
    }


    /**
     * 获取会员已申请的带工作地址的详情的页面
     * 通过JOB_ID重新刷新一下任务页面,但是这一次是会员带地址的
     */
    @Description("获取会员已申请的带工作地址的详情的页面")
    @GetMapping({"/detailsVip"})
    public R taskDetailsVIP(@RequestParam String jobId) {
        JobDetailVipVO jobDetailVO = jobTaskService.taskUserDetailVip(jobId);
        return R.ok().put("data", jobDetailVO);
    }


    /**
     * 显示该用户的接单数量
     */
    @Description("获取会员已申请的带工作地址的详情的页面")
    @GetMapping({"/myJobDisplay"})
    public R myJobDisplay() {
        MyJobNumDisplayVO vo = jobTaskService.myJobDisplay();
        return R.ok().put("data", vo);
    }


    /**
     * 显示该用户的接单数量
     */
    @Description("获取用户的昵称和图片")
    @GetMapping({"/getNicknameAndPicture"})
    public R getNicknameAndPicture() {
        NicknameAndPictureVO vo = userService.getNicknameAndPicture();
        return R.ok().put("data", vo);
    }


    /**
     * 用户意见
     */
    @Description("用户意见")
    @PostMapping({"/suggestion"})
    public R suggestion(@RequestBody SuggestionVO suggestionVO) {
        boolean result = userService.suggestion(suggestionVO);
        return R.ok().put("data", result);
    }


    /**
     * 修改用户的个人信息=>当用户选择保存的时候,保存用户的信息
     */
    @Description("修改用户个人信息")
    @PostMapping({"/修改用户个人信息"})
    public R modifyPersonalInfo(@RequestBody SuggestionVO suggestionVO) {
        boolean result = userService.suggestion(suggestionVO);
        return R.ok().put("data", result);
    }


    /**
     * 展示用户的个人信息=>首先对用户的信息进行展示=>个人信息展示在输入框当中
     */
    @Description("用户的信息进行展示")
    @GetMapping({"/displayPersonalInfo"})
    public R displayPersonalInfo() {
        UserDisplayInfoVO userDisplayInfoVO=  userService.displayPersonalInfo();
        return R.ok().put("data", userDisplayInfoVO);
    }


    /**
     * 手机动态码发送(修改手机号码)
     *
     * @param phone 手机号码
     * @return 校验结果
     */
    @Description("手机动态码发送(修改手机号码)")
    @GetMapping({"/sendPhoneDynamicCode/{phoneNum}"})
    public R sendPhoneDynamicCode(@PathVariable(value = "phoneNum")
                                  @Pattern(regexp = "^(1[3-9]\\d{9}|0\\d{9,10})$", message = "手机号格式不正确")
                                  String phone) {
        return iJobTaskService.sendPhoneDynamicCode(phone, SmsTemplateStatus.CHANGE_TEMPLATE)
                ? R.ok(SMS_SEND_SUCCESS) : R.ok(SMS_SEND_FAILED);
    }


    /**
     * 邮箱动态码发送(修改邮箱)
     *
     * @param email 邮箱
     * @return 校验结果
     */
    @Description("邮箱动态码发送(修改邮箱号码)")
    @GetMapping({"/sendEmailDynamicCode/{email}"})
    public R sendEmailDynamicCode(@PathVariable(value = "email") @Email(message = "请输入正确的邮箱格式") String email) {
        return iJobTaskService.sendCodeIfUserExists(email, SmsTemplateStatus.CHANGE_TEMPLATE)
                ? R.ok(SMS_SEND_SUCCESS) : R.ok(SMS_SEND_FAILED);
    }


    /**
     * 手机动态码发送(修改手机号码)
     *
     * @param phone 手机号码
     * @return 校验结果
     */
    @Description("手机动态码发送(修改手机号码)")
    @GetMapping({"/sendNewPhoneDynamicCode/{phoneNum}"})
    public R sendNewPhoneDynamicCode(@PathVariable(value = "phoneNum")
                                  @Pattern(regexp = "^(1[3-9]\\d{9}|0\\d{9,10})$", message = "手机号格式不正确")
                                  String phone) {
        // 校验DB中是否已经存在
        User one = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getPhone, phone));
        Assert.isTrue(Objects.isNull(one),"该手机已经被绑定");
        return iJobTaskService.sendPhoneDynamicCode(phone, SmsTemplateStatus.CHANGE_TEMPLATE)
                ? R.ok(SMS_SEND_SUCCESS) : R.ok(SMS_SEND_FAILED);
    }


    /**
     * 邮箱动态码发送(修改邮箱)
     *
     * @param email 邮箱
     * @return 校验结果
     */
    @Description("邮箱动态码发送(修改邮箱号码)")
    @GetMapping({"/sendNewEmailDynamicCode/{email}"})
    public R sendNewEmailDynamicCode(@PathVariable(value = "email") @Email(message = "请输入正确的邮箱格式") String email) {
        // 校验DB中是否已经存在
        User one = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getEmail, email));
        Assert.isTrue(Objects.isNull(one),"该邮箱已经被绑定");
        return iJobTaskService.sendCodeIfUserExists(email, SmsTemplateStatus.CHANGE_TEMPLATE)
                ? R.ok(SMS_SEND_SUCCESS) : R.ok(SMS_SEND_FAILED);
    }


    /**
     * 修改验证校验
     *
     * @param loginPhoneVO 手机或者是邮箱附带验证码
     * @return 校验结果
     */
    @Description("修改验证校验")
    @PostMapping("/dynamicCodeCheck")
    public R dynamicCodeCheck(@RequestBody LoginPhoneVO loginPhoneVO) {
        return R.ok().put("result", iJobTaskService.checkSmsCode(loginPhoneVO,SmsTemplateStatus.CHANGE_TEMPLATE));
    }


    /**
     * 修改用户的全部信息,包括邮箱和手机号,它在页面中只是能够对输入框进行修改,但是实际的修改是通过该保存按钮
     */
    @Description("用户的信息进行修改")
    @PostMapping({"/changePersonalInfo"})
    public R changePersonalInfo(@RequestBody ChangePersonalVO changePersonalVO) {
        return R.ok().put("data", userService.changePersonalInfo(changePersonalVO));
    }

    /**
     * 图片上传到云存储,并进行图片的展示
     */
    @Description("图片上传到云存储,进行一个临时存储")
    @PostMapping({"/uploadPicToStorage"})
    public R uploadPicToStorage(@RequestPart(value = "file", required = false) MultipartFile file)  throws IOException {
        return R.ok().put("data", userService.uploadPicToStorage(file));
    }

}
